import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class LoginPanel extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textField_1;
	private JPasswordField passwordField_1;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginPanel frame = new LoginPanel();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public LoginPanel() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 500, 270);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		final JPanel panel = new JPanel();
		contentPane.add(panel);
		FlowLayout fl_panel = new FlowLayout(FlowLayout.CENTER, 5, 5);
		panel.setLayout(fl_panel);

		final JLabel lblNewLabel = new JLabel("user");
		panel.add(lblNewLabel);

		textField_1 = new JTextField();
		panel.add(textField_1);
		textField_1.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("password");
		panel.add(lblNewLabel_1);

		passwordField_1 = new JPasswordField();
		passwordField_1.setColumns(10);
		panel.add(passwordField_1);

		JButton btnNewButton = new JButton("login");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				JPanel panel1 = new JPanel();
				contentPane.add(panel1);
				panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

				String username = new String(textField_1.getText());
				String password = new String(passwordField_1.getPassword());
				JLabel welcome = new JLabel("Welcome " + username);
				panel1.add(welcome);

				if (username.equals("karol") || password.equals("1234")) {
					JLabel loginPerformDialog = new JLabel("loading ...");
					JOptionPane.showMessageDialog(null, loginPerformDialog);
					panel.setVisible(false);
					panel1.setVisible(true);

				}

				else {
					JLabel errorDialog = new JLabel(
							"You must enter valid a username and password to login ");
					JOptionPane.showMessageDialog(null, errorDialog);
				}

			}
		});
		panel.add(btnNewButton);

	}

}
